package com.yun.blog.bean;

import com.google.gson.Gson;
import lombok.*;
import lombok.experimental.FieldNameConstants;

import java.io.Serializable;
import java.util.HashMap;

/**
 * title        : Msg
 * projectName  : ssm_demo
 * description  : TODO
 * author       : simple
 * date         : 2022/10/31下午12:37
 */


@Data
public class Result implements Serializable {

    private int code;
    private String msg;

    private HashMap<String, Object> dataMap;

    public Result() {
        this.dataMap = new HashMap<>();
    }

    public static Result success() {
        Result res = new Result();
        res.setCode(200);
        res.setMsg("success");
        return res;
    }

    public static Result success(String msg) {
        Result res = new Result();
        res.setCode(200);
        res.setMsg(msg);
        return res;
    }

    public static Result fail() {
        Result res = new Result();
        res.setCode(400);
        res.setMsg("fail");
        return res;
    }

    public static Result fail(String msg) {
        Result res = new Result();
        res.setCode(400);
        res.setMsg(msg);
        return res;
    }

    public Result add(String key, Object value) {
        this.dataMap.put(key, value);
        return this;
    }

    /**
     * 返回此对象的Json字符串
     *
     * @return  java.lang.String
     **/
    public String toJson(){
        Gson gson = new Gson();
        return gson.toJson(this);
    }


}
